package com.occludens.client.landing;


import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.inject.Inject;
import com.gwtplatform.dispatch.client.DispatchAsync;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.annotations.UseGatekeeper;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;
import com.occludens.client.ClientState;
import com.occludens.client.DispatchCallback;
import com.occludens.client.Main;
import com.occludens.client.main.MainPresenter;
import com.occludens.client.NameTokens;
import com.occludens.client.SignedInGatekeeper;
import com.occludens.client.event.UserInfoAvailableEvent;
import com.occludens.shared.dispatch.GetUserAction;
import com.occludens.shared.dispatch.GetUserResult;
import com.occludens.shared.model.UserInfo;

public class LandingPresenter extends Presenter<LandingPresenter.MyView, LandingPresenter.MyProxy> implements LandingUiHandlers {

	@ProxyStandard
	@NameToken(NameTokens.landing)
	//@UseGatekeeper(SignedInGatekeeper.class)
	public interface MyProxy extends ProxyPlace<LandingPresenter> {
	}

	public interface MyView extends View, HasUiHandlers<LandingUiHandlers> {
		public void setUserInfo(UserInfo userInfo);
	}

	private final PlaceManager placeManager;
	private final DispatchAsync dispatcher;
	private ClientState clientState;

	@Inject
	public LandingPresenter(EventBus eventBus, MyView view, MyProxy proxy, final PlaceManager placeManager, DispatchAsync dispatcher, final ClientState clientState) {
		super(eventBus, view, proxy);
		getView().setUiHandlers(this);

		this.placeManager = placeManager;
		this.dispatcher = dispatcher;
		this.clientState = clientState;
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, MainPresenter.TYPE_RevealPageContent, this);
		Main.logger.info("Revealing Landing");
	}

	@Override
	protected void onBind() {
		super.onBind();

		dispatcher.execute(new GetUserAction(Window.Location.getHref()),
				new DispatchCallback<GetUserResult>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
			}

			@Override
			public void onSuccess(GetUserResult result) {
				if (!result.getErrorText().isEmpty()) {
					// TODO have a general handler for this
					Window.alert(result.getErrorText());
					return;
				}
				clientState.userInfo = result.getUserInfo();
				onGetUserSuccess();
			}

		});

	}

	public void onGetUserSuccess() {
		Main.logger.info("onGetUserSuccess: User isSignedIn "
				+ Boolean.valueOf(clientState.userInfo.isSignedIn) + " with email "
				+ clientState.userInfo.email + " username "
				+ clientState.userInfo.userId);

		UserInfoAvailableEvent.fire(this, clientState);

		if (clientState.userInfo.isSignedIn) {
			PlaceRequest myRequest = new PlaceRequest(NameTokens.performances);
			placeManager.revealPlace(myRequest);
		} else {
			getView().setUserInfo(clientState.userInfo);
		}

	}

}
